﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataAccessLayer;
using System.Configuration;
using System.Data;

namespace DAL
{
    public class CommonDAL
    {
        private DBManager _dbmanager;

        #region Phương thức khởi tạo
        public CommonDAL()
        {
            string _connectionInfo = ConfigurationManager.AppSettings["ConnectionInfo"];
            _dbmanager = new DBManager(DataProvider.SqlServer, _connectionInfo);
        }
        #endregion

        #region Lấy các giá trị quy định trong bảng tham số
        // Lấy các giá trị quy định trong bảng tham số
        public int laySoLuotMuaGoiLaHot()
        {
            int soluotmua = 0;
            IDataReader reader = null;
            try
            {
                _dbmanager.Open();

                reader = _dbmanager.ExecuteReader(CommandType.Text, "select giatri from thamso where mathamso = 1");
                while(reader.Read())
                    soluotmua = Convert.ToInt32(reader["giatri"]);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                _dbmanager.Close();
            }
            return soluotmua;
        }

        #endregion

        #region LaySoSanPhamTrenTrang
        public int laySoSanPhamTrenTrang()
        {
            int sosp = 0;
            IDataReader reader = null;
            try
            {
                _dbmanager.Open();

                reader = _dbmanager.ExecuteReader(CommandType.Text, "select giatri from thamso where mathamso = 2");
                while (reader.Read())
                    sosp = Convert.ToInt32(reader["giatri"]);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                _dbmanager.Close();
            }
            return sosp;
        }
        #endregion

        #region Phương thức getDataSet
        /// <summary>
        /// Phương thức getDataSet
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns>DataSet</returns>
        public DataSet getDataSet(string tableName)
        {
            DataSet ds = new DataSet();
            try
            {
                _dbmanager.Open();
                ds = _dbmanager.ExecuteDataSet(CommandType.StoredProcedure, "usp_getList" + tableName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                _dbmanager.Close();
            }
            return ds;
        }

        #endregion
    }
}